<?php
namespace Admin\Models\server;

use Admin\Models\BaseModel;
use InfluxDB\Client;
use Admin\Models\sys\XhsysConfig;

class BoxNet extends BaseModel{

  public static $line_type = [
    'cucc' => '联通',
    'ctcc' => '电信',
    'cmcc' => '移动'
  ];

  /**
   * [search description]
   * @method search
   * @param  [type] $parameters [参数]
   * @return [array]             [数据数组/总条数]
   */
  public static function search($parameters = null){


    $config = XhsysConfig::findFirst("sConfigName = 'box_log' AND bDelete = 0");
    if(is_bool($config)){
      return array();
    }
    else{
      $configData = json_decode($config->mConfigValue);
      $host = $configData->host;
      $port = $configData->port;
      $username = $configData->username;
      $password = $configData->password;
      $dbname = 'telegraf';
    }

    $client = new Client($host,$port,$username,$password);
    $database = $client->selectDB($dbname);
    $query = $database->getQueryBuilder()->select('*')->from('box_net_stat');

    $database2 = clone($database);
    $queryForCount = $database2->getQueryBuilder()->count('School_ID')->from('box_net_stat');

    if(isset($parameters['schoolname'])){
        if($parameters['schoolname']){
          $query->where(['School_Name =~ /.*'.$parameters['schoolname'].'.*/']);
          $queryForCount->where(['School_Name =~ /.*'.$parameters['schoolname'].'.*/']);
        }
    }

    if(isset($parameters['schoolid'])){
        if($parameters['schoolid']){
          $query->where(['School_ID ='.$parameters['schoolid']]);
          $queryForCount->where(['School_ID ='.$parameters['schoolid']]);
        }
    }

    $totalData = $queryForCount->getResultSet()->getPoints();

    $total = 0;
    if(isset($totalData[0])){
      $total = $totalData[0]['count'];
    }

    if(isset($parameters['page'])){
        $page = $parameters['page'];
    }
    else{
        $page = 1;
    }

    if(isset($parameters['rows'])){
        $rows = intval($parameters['rows']);
    }
    else{
        $rows = 2;
    }

    if (isset($parameters['mintime']) && $parameters['mintime']) {
      $days = intval((time()-strtotime($parameters['mintime']))/86400)+1;
      $query->where(['time > now() -'.$days.'d']);
    }
    if (isset($parameters['maxtime']) && $parameters['maxtime']) {
        $days2 = intval((time()-strtotime($parameters['maxtime']))/86400);
        $query->where(['time < now() -'.$days2.'d']);
    }

    $query->orderBy('time','desc');
    $query->limit($rows);

    $queryStr = $query->getQuery();
    $queryStr .= ' OFFSET '.($page-1)*$rows;
    $rows = $database->query($queryStr)->getPoints();

    foreach($rows as $key => $val){
      if(array_key_exists($val['Line_Type'],self::$line_type)){
        $rows[$key]['Line_Type'] = self::$line_type[$val['Line_Type']];
      }

      if($val['Is_Default_Line'] === 'true'){
        $rows[$key]['Is_Default_Line'] = '是';
      }
      else{
        $rows[$key]['Is_Default_Line'] = '否';
      }

      $rows[$key]['time'] = date('Y-m-d H:i:s', strtotime(substr($val['time'],0, strrpos($val['time'],'.')) .'+8 hours'));
    }

    $result = array();
    $result['rows'] = $rows;
    $result['total'] = $total;
    return $result;
  }

}
